﻿using MMK.Paket_Tracker.Runtime;
using System.Windows.Controls;
using System.Windows.Input;
using MMK.Paket_Tracker.Runtime.Commands;
using MMK.Paket_Tracker.Runtime.BusinessEntities;
using MMK.Paket_Tracker.GUI.Models;
using MMK.Paket_Tracker.GUI.BusinessLogic;
using System;
using MMK.Paket_Tracker.Runtime.Interfaces;

namespace MMK.Paket_Tracker.GUI.ViewModels
{
    /// <summary>
    /// View model for NewPacketAddressSender view
    /// </summary>
    public class NewPacketAddressSenderViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="NewPacketAddressSenderViewModel"/> class.
        /// </summary>
        /// <param name="owner">The owner.</param>
        public NewPacketAddressSenderViewModel(UserControl owner)
        {
            this.owner = owner;
            PrepareCommands();
            if (BusinessLogic.BusinessLogic.SenderModel == null)
            {
                this.model = new SenderModel();
            }
            else
            {
                this.model = BusinessLogic.BusinessLogic.SenderModel;
            }
        }

        /// <summary>
        /// Gets or sets the data for datacontext.
        /// </summary>
        /// <value>The data.</value>
        public SenderModel Data
        {
            get { return model; }
            set { model = value; }
        }
        


        private void PrepareCommands()
        {
            CommandBinding goOn = new CommandBinding(PTCommands.GoOn);
            CommandBinding goBack = new CommandBinding(PTCommands.GoBack);

            goOn.Executed += goOn_Executed;
            goOn.CanExecute += goOn_CanExecute;

            goBack.Executed += goBack_Executed;
            goBack.CanExecute += goBack_CanExecute;

            owner.CommandBindings.Add(goOn);
            owner.CommandBindings.Add(goBack);
        }

        void goBack_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        void goBack_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            BusinessLogic.BusinessLogic.SenderModel = null;
            NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
        }

        void goOn_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            if (model.HasError)
            {
                ((INamedUserControl)owner).ErrorInfo = model.ErrorDictionary;
            }
        }

        void goOn_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (model.HasError == false)
            {
                BusinessLogic.BusinessLogic.SenderModel = Data;
                NotifyMainWindow(new MainWindowNotification(((UserControl)sender).Name, null, (RoutedUICommand)e.Command));
            }
        }


        private SenderModel model;
    }
 

}
